from .models import Goods, GoodsCategory,HotSearchWords,GoodsImage,Banner

from rest_framework import serializers

class CategorySerializers(serializers.ModelSerializer):
    class Meta:
        model = GoodsCategory       #序列化GoodsCategory模型
        fields = "__all__"

class GoodsImagesSerializers(serializers.ModelSerializer):
    """
    轮播图序列化
    """
    class Meta:
        model = GoodsImage
        fields = ("image",)#序列化inage字段


class GoodsSerializers(serializers.ModelSerializer):  # 继承serializers.ModelSerializer
    """
    商品序列化
    """
    category = CategorySerializers()    #序列化后的GoodsCategory模型数据装入category字段,
    #反向序列化，该字段名是外键中related_name="images"
    images = GoodsImagesSerializers(many=True)                #将轮播图序列化后加入到商品中

    class Meta:
        model = Goods
        fields = "__all__"  # 序列化所有字段，也可自定义字段['name','dse']





class GoodsCategorySerializers3(serializers.ModelSerializer):
    """
    三级商品类目序列化
    """
    class Meta:
        model = GoodsCategory
        fields = "__all__"#序列化所有字段

class GoodsCategorySerializers2(serializers.ModelSerializer):
    """
    二级商品类目序列化
    """
    sub_cat = GoodsCategorySerializers3(many=True)

    class Meta:
        model = GoodsCategory
        fields = "__all__"  #序列化所有字段


class GoodsCategorySerializers(serializers.ModelSerializer):
    """
    一级商品类目序列化
    """
    sub_cat = GoodsCategorySerializers2(many=True)      #many=True该对象中有多条数据不写会报错
    class Meta:
        model = GoodsCategory       #模型
        fields = "__all__"          #序列化所有字段




class HotWordsSerializer(serializers.ModelSerializer):
    """
    搜索栏下方热搜词
    """
    class Meta:
        model = HotSearchWords       #模型
        fields = "__all__"          #序列化所有字段

class BannerSerializer(serializers.ModelSerializer):
    """
    首页轮播图
    """
    class Meta:
        model = Banner  # 模型
        fields = "__all__"  # 序列化所有字段

class NewGoodsSerializer(serializers.ModelSerializer):
    """
    新品
    """
    class Meta:
        model = Goods  # 模型
        fields = "__all__"  # 序列化所有字段